//
//  BaseViewController.m
//  TaurusXi
//
//  Created by xicheng on 15/11/27.
//  Copyright © 2015年 xicheng. All rights reserved.
//

#import "BaseViewController.h"
#import "DisplayUtils.h"
#import <MBProgressHUD/MBProgressHUD.h>
#define NavigationBar_HEIGHT                64
#import "UINavigationController+FDFullscreenPopGesture.h"
@interface BaseViewController ()

@property (nonatomic,strong) MBProgressHUD *hud;

@end

@implementation BaseViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [self initFrame];
  self.fd_prefersNavigationBarHidden = YES;
  [self creatCustomNavigationBar];
  // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

- (void)initFrame {
  _deviceFrame = [DisplayUtils deviceBounds];
  _deviceWidth = _deviceFrame.size.width;
  _deviceHeight = _deviceFrame.size.height;
}

//自定义导航栏
- (void)creatCustomNavigationBar
{
  self.customNavigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.deviceWidth, NavigationBar_HEIGHT)];
  self.customNavigationBar.backgroundColor = [UIColor colorWithRed:4/255. green:26/255. blue:66/255. alpha:0.77];
  UIView * mView=[[UIView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, self.deviceWidth, NavigationBar_HEIGHT-kStatusBarHeight)];//这是整个navigationBar的颜色
  mView.backgroundColor = [UIColor clearColor];
//  mView.backgroundColor = [UIColor yellowColor];
  [self.customNavigationBar insertSubview:mView atIndex:0];
  
  [self.customNavigationBar setClipsToBounds:YES];//去除naviBar黑线
  //添加标题
  [mView addSubview:self.titleLabel];
  self.titleLabel.center = CGPointMake(mView.center.x, mView.center.y-kStatusBarHeight);
  //添加左侧返回按钮
  [mView addSubview:self.leftBarButton];
  
  self.leftBarButton.center = CGPointMake(30, mView.frame.size.height/2);
  //添加右侧功能按钮
  [mView addSubview:self.rightBarButton];
  //添加自定义导航栏
  [self.view addSubview:self.customNavigationBar];
  
  //添加是为了生效全屏侧滑
  self.navigationController.navigationBarHidden = YES;
}


-(void) setTitleText:(NSString *)text{
  self.titleLabel.text= text;
  [self.titleLabel sizeToFit];
  self.titleLabel.center = CGPointMake(self.deviceWidth/2, 44/2);
  
}

-(void) viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.tabBarController.tabBar.hidden = YES;
}

-(void) viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  self.tabBarController.tabBar.hidden = NO;
}




#pragma mark lazyload
-(UIButton *)leftBarButton
{
  if (nil == _leftBarButton) {
    self.leftBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
    [_leftBarButton addTarget:self action:@selector(leftBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    UIImageView *leftBarIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 12, 20)];
//    leftBarIcon.image = nil;
//    
//    leftBarIcon.tag = 100;
    [_leftBarButton setImage:[UIImage imageNamed:@"ic_exchange_left"] forState:UIControlStateNormal];
    [_leftBarButton setImage:[UIImage imageNamed:@"ic_exchange_left_icon_click"] forState:UIControlStateHighlighted];
//    [_leftBarButton insertSubview:leftBarIcon atIndex:0];
//    [_leftBarButton sizeToFit];
  }
  return _leftBarButton;
}

- (UIButton *)rightBarButton
{
  if (nil == _rightBarButton) {
    self.rightBarButton = [[UIButton alloc] initWithFrame:CGRectMake(self.deviceWidth-62, 30, 60, 30)];
    [_rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _rightBarButton.titleLabel.font = [UIFont systemFontOfSize:15];
  }
  return _rightBarButton;
}

- (UILabel *)titleLabel
{
  if (nil == _titleLabel) {
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 26, self.deviceWidth-100, 30)];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
  }
  return _titleLabel;
}

- (void)leftBarButtonAction:(id)sender
{
  [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarButtonAction:(id)sender
{
  
}


- (void)showSimplePro{

  if (self.hud!=nil && self.hud.superview!=nil) {
    return;
  }
  self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  self.hud.labelText = @"加载中...";
  self.hud.removeFromSuperViewOnHide = YES;
}

- (void)hideSimpleProWithDelay:(NSTimeInterval)delay{
  [self.hud hide:YES afterDelay:delay];
}

- (void)showSimpleProWithDim{
  
  if (self.hud!=nil && self.hud.superview!=nil) {
    return;
  }
  self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  self.hud.labelText = @"加载中...";
  self.hud.dimBackground = YES;
  self.hud.removeFromSuperViewOnHide = YES;
}


#pragma mark 弹出框
- (MBProgressHUD*)showPop:(NSString*)title{
  
  if (self.hud!=nil && self.hud.superview!=nil) {
    return self.hud;
  }
  self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  self.hud.mode      = MBProgressHUDModeText;
  self.hud.labelText = title;
  self.hud.minSize   = CGSizeMake(180, 30);
  self.hud.margin    = 10;
  if (self.deviceHeight == 480) {
    self.hud.yOffset   = 30;
  }else{
    self.hud.yOffset   = -20;
  }
  self.hud.removeFromSuperViewOnHide = YES;
  [self.hud hide:YES afterDelay:1.5];
  return self.hud;
}


@end
